package py.com.alfa.escriba.web.rest.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import py.com.alfa.escriba.config.EscribaApplicationContextProvider;
import py.com.alfa.escriba.config.HttpHeaderConfig;
import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dto.ControlesDTO;
import py.com.alfa.escriba.persistence.dto.ControlesEscriturasDTO;
import py.com.alfa.escriba.persistence.dto.ControlesFormulariosInfoDTO;
import py.com.alfa.escriba.persistence.dto.ControlesInfoDTO;
import py.com.alfa.escriba.persistence.dto.ControlestextoDTO;
import py.com.alfa.escriba.persistence.dto.query.ControlesQueryDTO;
import py.com.alfa.escriba.persistence.dto.query.ControlesRequestDTO;
import py.com.alfa.escriba.web.interceptor.BeanNames;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.rest.dto.GenerateEscrituraDTO;
import py.com.alfa.escriba.web.rest.secure.ControlesSecureRESTService;
import py.com.alfa.escriba.web.services.ControlesService;
import py.com.alfa.escriba.web.services.GenerateEscrituraService;

public class ControlesSecureRESTServiceImpl implements ControlesSecureRESTService{

	private ApplicationContext ctx;
	private ControlesService dao;
	private GenerateEscrituraService generate;
	private InvocationContext ic;
	private HttpHeaderConfig config;
	private Logger logger = LoggerFactory.getLogger(getClass());

	public ControlesSecureRESTServiceImpl() {
		this.ctx = EscribaApplicationContextProvider.getContext();
		this.dao = (ControlesService) ctx.getBean("controlesService");
		this.generate = (GenerateEscrituraService)ctx.getBean("generateEscrituraService");
		this.config = (HttpHeaderConfig) ctx.getBean(BeanNames.HTTP_HEADERS_BEAN);
	}



	@Override
	public List<ControlesDTO> listControles(HttpServletRequest request) throws ServiceException{
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			List<ControlesDTO> result = dao.listControles(ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}


	@Override
	public ControlesInfoDTO showControl(Long id, HttpServletRequest request)
			throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			ControlesInfoDTO result = dao.getControles(id, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}


	@Override
	public ControlesInfoDTO saveControl(Long id, ControlesInfoDTO dto,
			HttpServletRequest request) throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			if(ic.getIdEscribano()!=null || !ic.getIdEscribano().equals("")){
				   dto.setIdescribano(ic.getIdEscribano());
			}
			ControlesInfoDTO result = dao.updateControles(id, dto, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}


	@Override
	public ControlesInfoDTO createControl(ControlesInfoDTO dto,
			HttpServletRequest request) throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			ControlesInfoDTO result = dao.createControles(dto, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}

	@Override
	public Response deleteControl(Long id, HttpServletRequest request)
			throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			dao.deleteControles(id, ic);
			return Response.ok().build();
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}
	
	@Override
	public List<ControlesRequestDTO> listRequestControles(ControlesQueryDTO dto, HttpServletRequest request) throws ServiceException{
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			List<ControlesRequestDTO> result = dao.listRequestControles(dto,ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}



	@Override
	public List<ControlesFormulariosInfoDTO> getControlesFormulariosByIdControl(
			Long idControl, HttpServletRequest request) throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			List<ControlesFormulariosInfoDTO> result = dao.getControlesFormulariosByIdControl(idControl, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}



	@Override
	public ControlesFormulariosInfoDTO getControlesFormulariosById(
			Long id, HttpServletRequest request) throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			ControlesFormulariosInfoDTO result = dao.getControlesFormulariosById(id, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}



	@Override
	public ControlesInfoDTO saveControlesFormularios(Long idControl,
			ControlesInfoDTO dto, HttpServletRequest request)
			throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			if(ic.getIdEscribano()!=null || !ic.getIdEscribano().equals("")){
				   dto.setIdescribano(ic.getIdEscribano());
			}
			ControlesInfoDTO result = dao.updateControlesFormularios(idControl, dto, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}



	@Override
	public List<ControlesEscriturasDTO> getControlesEscriturasByIdControl(
			Long idControl, HttpServletRequest request) throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			List<ControlesEscriturasDTO> result = dao.listControlesEscrituraByIdControl(idControl, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}



	@Override
	public ControlesEscriturasDTO showControlesEscritura(Long id,
			HttpServletRequest request) throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			ControlesEscriturasDTO result = dao.getControlesEscritura(id, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}



	@Override
	public ControlesInfoDTO saveControlesEscrituras(Long idControl,
			ControlesInfoDTO dto, HttpServletRequest request)
			throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			if(ic.getIdEscribano()!=null || !ic.getIdEscribano().equals("")){
				   dto.setIdescribano(ic.getIdEscribano());
			}
			ControlesInfoDTO result = dao.updateControlesEscritura(idControl, dto, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}



	@Override
	public GenerateEscrituraDTO generateEscrituras(Long idControl, Long idTemplate, HttpServletRequest request)
			throws ServiceException {
		GenerateEscrituraDTO res = new GenerateEscrituraDTO(idControl, idTemplate);
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			res.setDocumento(this.generate.convert(idControl, idTemplate));
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		return res;
	}



	@Override
	public ControlestextoDTO getControlesTextoByIdControl(Long idControl,
			HttpServletRequest request) throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			ControlestextoDTO result = dao.getControlesTextoByIdControl(idControl, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}



	@Override
	public ControlestextoDTO showControlesTexto(Long id,
			HttpServletRequest request) throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			ControlestextoDTO result = dao.getControlesTexto(id, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}



	@Override
	public ControlesInfoDTO saveControlesTexto(Long idControl,
			ControlesInfoDTO dto, HttpServletRequest request)
			throws ServiceException {
		try {
			ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
			if(ic.getIdEscribano()!=null || !ic.getIdEscribano().equals("")){
				   dto.setIdescribano(ic.getIdEscribano());
			}
			ControlesInfoDTO result = dao.updateControlesTexto(idControl, dto, ic);
			return result;
		}
		catch (ServiceException e){
			logger.error(e.getMessage(), e);
			throw e;
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
			throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
		}
	}


}